package com.siyougame.business.controller.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.siyougame.business.controller.admin.template.vo.*;
import com.siyougame.business.controller.admin.templateItem.vo.TemplateItemPageReqVO;
import com.siyougame.business.controller.admin.templateItem.vo.TemplateItemRespVO;
import com.siyougame.business.controller.app.vo.AppTemplateItemPageReqVO;
import com.siyougame.business.controller.app.vo.AppTemplateItemRespVO;
import com.siyougame.business.controller.app.vo.AppTemplatePageReqVO;
import com.siyougame.business.controller.app.vo.AppTemplateRespVO;
import com.siyougame.business.convert.template.TemplateConvert;
import com.siyougame.business.convert.templateItem.TemplateItemConvert;
import com.siyougame.business.dal.dataobject.template.TemplateDO;
import com.siyougame.business.dal.dataobject.templateItem.TemplateItemDO;
import com.siyougame.business.enums.TemplateStatusEnum;
import com.siyougame.business.service.template.TemplateService;
import com.siyougame.business.service.templateItem.TemplateItemService;
import com.siyougame.common.annotation.OperateLog;
import com.siyougame.common.pojo.CommonResult;
import com.siyougame.common.pojo.PageResult;
import com.siyougame.common.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.siyougame.common.enums.OperateTypeEnum.EXPORT;
import static com.siyougame.common.pojo.CommonResult.success;

@Api(tags = "模板")
@RestController
@RequestMapping("/openapi/app/template")
@Validated
@SaCheckLogin
public class AppTemplateController {

    @Resource
    private TemplateService templateService;
    @Resource
    private TemplateItemService templateItemService;

    @GetMapping("/get")
    @ApiOperation("获得模板详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<AppTemplateRespVO> getTemplate(@RequestParam("id") Long id) {
        TemplateDO template = templateService.getOne(Wrappers.<TemplateDO>lambdaQuery().eq(TemplateDO::getId, id)
                .eq(TemplateDO::getStatus, TemplateStatusEnum.NORMAL.getCode()));
        return success(TemplateConvert.INSTANCE.convertApp(template));
    }
    @GetMapping("/page")
    @ApiOperation("获得模板分页")
    public CommonResult<PageResult<AppTemplateRespVO>> getTemplatePage(@Valid AppTemplatePageReqVO pageVO) {
        PageResult<TemplateDO> pageResult = templateService.getTemplatePage(pageVO);
        return success(TemplateConvert.INSTANCE.convertAppPage(pageResult));
    }

    @GetMapping("/pageItem")
    @ApiOperation("根据模板id获得二级模板明细分页")
    public CommonResult<PageResult<AppTemplateItemRespVO>> getTemplateItemPage(@Valid AppTemplateItemPageReqVO pageVO) {
        PageResult<TemplateItemDO> pageResult = templateItemService.getTemplateItemPage(pageVO);
        return success(TemplateItemConvert.INSTANCE.convertAppPage(pageResult));
    }

}
